package Logic

import (
	"fmt"
	"math"
	"strconv"
	"strings"
	"time"
	"yz-go/Common"
	"yz-go/Models"
	"yz-go/Requests"

	"gorm.io/gorm"
)

func GetNotes(page int, size int, w map[string]interface{}) (rd Common.ResData) {
	var jobs []Models.RecruitJob
	var totalRows int64
	// 查询总行数
	// var w = map[string]interface{}{"id": 2} 示例
	errCount := Models.DB.Model(&Models.RecruitJob{}).Where(w).Count(&totalRows).Error
	if errCount != nil {
		fmt.Println(errCount)
		// 处理错误情况
		return Common.ResData{Code: Common.ERROR, Msg: errCount}
	}

	// 计算总页数
	totalPages := int(math.Ceil(float64(totalRows) / float64(size)))

	// 进行分页查询
	offset := (page - 1) * size
	errQuery := Models.DB.Debug().Where(w).Limit(size).Offset(offset).Find(&jobs).Error
	if errQuery != nil {
		fmt.Println(errQuery)
		// 处理错误情况
		return Common.ResData{Code: Common.ERROR, Msg: errQuery}
	}
	data := map[string]interface{}{"page": page, "size": size, "total": totalPages, "list": jobs}
	res := Common.ResData{Code: Common.SUCCESS, Msg: "", Data: data}
	return res
}

func CreateNotes(paramsa Requests.NoteParams, id int64) (rd Common.ResData) {
	fmt.Println("------------logic-------------")
	fmt.Println(paramsa)

	TX := Models.DB.Begin()
	fmt.Println("note")

	currentTime := time.Now().Unix()
	workYear := int64(math.Ceil(float64(Common.TimeDiff(currentTime, int64(paramsa.WorkSt)/1000)["day"]) / 365))

	//var note Requests.NoteInsertData
	//note.Adv = paramsa.Adv
	//note.UserName = paramsa.UserName
	//note.WorkSt = paramsa.WorkSt
	//note.Email = paramsa.Email
	//note.Birth = paramsa.Birth / 1000
	//note.WorkDate = paramsa.WorkDate / 1000
	//note.WeChat = paramsa.WeChat
	//note.Sex = paramsa.Sex
	//note.Mobile = id
	//note.CellMobile = paramsa.Mobile
	//note.Adv = paramsa.Adv
	//note.CreatedAt = time.Now().Unix()
	//
	//fmt.Println(note)
	//
	noteId := 0
	mobile := paramsa.Mobile
	//bool := Models.DB.Table("recruit_notes").Create(&note).Error
	//fmt.Println(bool)
	//if bool != nil {
	//	TX.Rollback()
	// return rd
	//}

	// 教育
	//eduR := createEducationalBack(TX, paramsa, noteId, mobile)
	//if eduR == Common.ERROR {
	//	TX.Rollback()
	//	rd.Code = 300
	//	rd.Msg = "异常"
	//	return rd
	//}

	// 意向
	// 把工作日期 和当前时间对比 转为具体年份
	intenR := createreNotesIntention(TX, paramsa, noteId, mobile, workYear)
	if intenR == Common.ERROR {
		TX.Rollback()
		rd.Code = 300
		rd.Msg = "异常"
		return rd
	}

	// 添加工作经验
	workR := createWorkExp(TX, paramsa, noteId, mobile)
	if workR == Common.ERROR {
		TX.Rollback()
		rd.Code = 300
		rd.Msg = "异常"
		return rd
	}

	// 添加项目经验
	proR := createProjectExp(TX, paramsa, noteId, mobile)
	if proR == Common.ERROR {
		TX.Rollback()
		rd.Code = 300
		rd.Msg = "异常"
		return rd
	}

	TX.Commit()
	rd.Code = Common.SUCCESS
	return rd
}

// 教育背景写入
func createEducationalBack(TX *gorm.DB, eduBack Requests.NoteParams, noteId int, mobile string) (st int) {
	if len(eduBack.EducationalBackData) == 0 {
		return Common.SUCCESS
	}
	var eduBackInsertData []Requests.EducationalBackData
	var tmp Requests.EducationalBackData

	for _, v := range eduBack.EducationalBackData {
		tmp.JobNotesId = noteId
		tmp.Education = v.Education
		tmp.Speciality = v.Speciality
		tmp.StDateSt = v.StDateSt / 1000
		tmp.StDateEd = v.StDateEd / 1000
		tmp.UniversityName = v.UniversityName
		tmp.Mobile = mobile
		tmp.CreatedAt = time.Now().Unix()
		eduBackInsertData = append(eduBackInsertData, tmp)
	}

	bool := TX.Table("recruit_educational_back").Create(&eduBackInsertData).Error

	if bool != nil {
		return 300
	}
	return 200
}

// 意向
func createreNotesIntention(TX *gorm.DB, eduBack Requests.NoteParams, noteId int, mobile string, workYear int64) (st int) {

	if len(eduBack.NotesIntention) == 0 {
		return Common.SUCCESS
	}

	var eduBackInsertData []Requests.NotesIntentionData
	var tmp Requests.NotesIntentionData
	var educationIds []string

	// 获取学历id
	for _, v := range eduBack.EducationalBackData {
		educationIds = append(educationIds, strconv.Itoa(v.Education))
	}
	educationJoined := strings.Join(educationIds, ",")
	fmt.Println(educationIds)

	// 意向
	for _, v := range eduBack.NotesIntention {
		tmp.JobNotesId = noteId
		tmp.Mobile = mobile
		tmp.PositionCategoryId = v.PositionCategoryId
		tmp.Education = educationJoined
		tmp.JobType = v.JobType
		tmp.Salary1 = v.Salary1
		tmp.Salary2 = v.Salary2
		tmp.WorkYear = workYear
		tmp.WorkProvince = v.WorkProvince
		tmp.WorkCity = v.WorkCity
		tmp.CreatedAt = time.Now().Unix()
		eduBackInsertData = append(eduBackInsertData, tmp)
	}

	bool := TX.Table("recruit_notes_intention").Create(&eduBackInsertData).Error

	if bool != nil {
		return 300
	}
	return 200
}

// 工作经验
func createWorkExp(TX *gorm.DB, eduBack Requests.NoteParams, noteId int, mobile string) (st int) {

	if len(eduBack.WorkExp) == 0 {
		return Common.SUCCESS
	}
	var workExpInsertData []Requests.WorkExpData
	var tmp Requests.WorkExpData

	// 工作经验
	for _, v := range eduBack.WorkExp {
		tmp.JobNotesId = noteId
		tmp.Mobile = mobile
		tmp.Salary = v.Salary
		tmp.BusinessName = v.BusinessName
		tmp.Tp = Requests.WORK
		tmp.PositionName = v.PositionName
		tmp.WorkExp = v.WorkExp
		tmp.WorkDateSt = v.WorkDateSt / 1000
		tmp.WorkDateEd = v.WorkDateEd / 1000
		tmp.CreatedAt = time.Now().Unix()
		workExpInsertData = append(workExpInsertData, tmp)
	}

	bool := TX.Table("recruit_work_exp").Create(&workExpInsertData).Error

	if bool != nil {
		return Common.ERROR
	}
	return Common.SUCCESS
}

// 项目经验
func createProjectExp(TX *gorm.DB, eduBack Requests.NoteParams, noteId int, mobile string) (st int) {

	if len(eduBack.ProjectExp) == 0 {
		return Common.SUCCESS
	}
	var ProjectExpInsertData []Requests.ProjectExpData
	var tmp Requests.ProjectExpData

	// 项目经验
	for _, v := range eduBack.ProjectExp {
		tmp.JobNotesId = noteId
		tmp.Mobile = mobile
		tmp.BusinessName = v.BusinessName
		tmp.Tp = Requests.PROJECT
		tmp.PositionName = v.PositionName
		tmp.WorkExp = v.WorkExp
		tmp.WorkName = v.WorkName
		tmp.WorkDateSt = v.WorkDateSt / 1000
		tmp.WorkDateEd = v.WorkDateEd / 1000
		tmp.CreatedAt = time.Now().Unix()
		ProjectExpInsertData = append(ProjectExpInsertData, tmp)
	}

	bool := TX.Table("recruit_work_exp").Create(&ProjectExpInsertData).Error

	if bool != nil {
		return 300
	}
	return 200
}

func GetNoteByMobile(mobile string) (rd Common.ResData) {

	var data Requests.NoteParams
	var Basic Requests.Basic
	var EducationalBacks []Requests.EducationalBackData

	errQuery := Models.DB.Table("recruit_notes").Where("mobile = ?", mobile).Find(&Basic).Error
	if errQuery != nil {
		fmt.Println(errQuery)
		return Common.ResData{Code: Common.ERROR, Msg: errQuery}
	}

	// 获取学历
	errEdu := Models.DB.Table("recruit_educational_back").Where("mobile = ?", mobile).Find(&EducationalBacks).Error
	if errEdu != nil {
		fmt.Println(errEdu)
		return Common.ResData{Code: Common.ERROR, Msg: errEdu}
	}

	data.Basic = Basic
	data.EducationalBackData = EducationalBacks
	res := Common.ResData{Code: Common.SUCCESS, Msg: "", Data: data}
	return res
}
